package com.treasure.mall.zbq.common;


import com.treasure.mall.zbq.common.constant.Constant;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.concurrent.TimeUnit;

/**
 * jdk8日期时间工具类
 *
 * @author allan
 * @date 2019-08-16
 */
public class Jsr310Utils {
    public static class Date {
        public static String asString(LocalDate date, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return formatter.format(date);
        }

        public static String asString(LocalDate date) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constant.DATE_FORMAT);

            return formatter.format(date);
        }

        public static LocalDate of(String str, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);

            return LocalDate.parse(str, formatter);
        }

        public static LocalDate of(String str) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constant.DATE_FORMAT);

            return LocalDate.parse(str, formatter);
        }

        public static LocalDate startOfMounth(LocalDate date) {
            return date.with(TemporalAdjusters.firstDayOfMonth());
        }

        public static LocalDate lastOfMounth(LocalDate date) {
            return date.with(TemporalAdjusters.lastDayOfMonth());
        }

        public static LocalDateTime startOfDay(LocalDate date) {
            return date.atStartOfDay();
        }

        public static LocalDateTime endOfDay(LocalDate date) {
            return LocalDateTime.of(date, LocalTime.MAX);
        }
    }

    public static class DateTime {
        /**
         * 转换成String
         *
         * @param dateTime
         * @param pattern
         * @return
         */
        public static String asString(LocalDateTime dateTime, String pattern) {
            if (dateTime == null) {
                return null;
            }
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
            return dateTimeFormatter.format(dateTime);
        }

        /**
         * 转换成String
         * pattern: yyyy-MM-dd HH:mm:ss
         *
         * @param dateTime
         * @return
         */
        public static String asString(LocalDateTime dateTime) {
            if (dateTime == null) {
                return null;
            }
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(Constant.DATETIME_FORMAT);
            return dateTimeFormatter.format(dateTime);
        }

        /**
         * String to LocalDateTime
         *
         * @param str
         * @param pattern
         * @return
         */
        public static LocalDateTime of(String str, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(str, formatter);
        }

        /**
         * String to {@link LocalDateTime}
         * pattern: yyyy-MM-dd HH:mm:ss
         *
         * @param str
         * @return
         */
        public static LocalDateTime of(String str) {
            return of(str, Constant.DATETIME_FORMAT);
        }

        public static boolean equalTo(LocalDateTime a, LocalDateTime b) {
            return a.compareTo(b) == 0;
        }

        public static LocalDateTime startOfDay(LocalDateTime time) {
            return LocalDateTime.of(time.toLocalDate(), LocalTime.MIN);
        }

        public static LocalDateTime lastOfDay(LocalDateTime time) {
            return LocalDateTime.of(time.toLocalDate(), LocalTime.MAX);
        }

        /**
         * convert to {@link java.util.Date}
         *
         * @param dateTime
         * @return
         */
        public static java.util.Date toDate(LocalDateTime dateTime) {
            ZoneId zoneId = ZoneId.systemDefault();
            ZonedDateTime zdt = dateTime.atZone(zoneId);

            return java.util.Date.from(zdt.toInstant());
        }

        public static long duration(LocalDateTime begin, LocalDateTime end, TimeUnit timeUnit) {
            java.time.Duration duration = java.time.Duration.between(begin, end);

            switch (timeUnit) {
                case MINUTES:
                    return duration.toMinutes();
                case HOURS:
                    return duration.toHours();
                case DAYS:
                    return duration.toDays();
                case SECONDS:
                    return TimeUnit.MILLISECONDS.toSeconds(duration.toMillis());
                default:
                    return duration.toMillis();
            }
        }
    }

    /**
     * 时长
     */
    public static class Duration {

        public static String getDurationBySeconds(long seconds) {
            if (seconds < 60) {//秒
                return seconds + "秒";
            }

            if (seconds < 3600) {//分

                return seconds / 60 + "分" + seconds % 60 + "秒";
            }

            if (seconds < 86400) {//时

                return seconds / 60 / 60 + "小时" + seconds / 60 % 60 + "分" + seconds % 60 + "秒";
            }

            return seconds / 60 / 60 / 24 + "天" + seconds / 60 / 60 % 24 + "小时" + seconds / 60 % 60 + "分" + seconds % 60 + "秒";
        }
    }
}
